package com.wt.demo;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.sl.usermodel.*;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public abstract class PPTUtils {
    @SneakyThrows
    public static Result getContent(SlideShow slideShow) {
        Assert.notNull(slideShow, "ppt内容不能为空");
        Result result = new Result();
        result.textList = new ArrayList<>();
        result.picPathList = new ArrayList<>();
        for (Slide slide : (List<Slide>) slideShow.getSlides()) {
            List shapes = slide.getShapes();
            if (shapes.size() == 0) {
                continue;
            }
            for (int i = 0; i < shapes.size(); i++) {
                Shape shape = (Shape) shapes.get(i);
                if (shape instanceof HSLFTextShape || shape instanceof XSLFTextShape) {
                    result.textList.add(((TextShape<?, ?>) shape).getText());
                }
            }
        }
        List pictures = slideShow.getPictureData();
        for (int i = 0; i < pictures.size(); i++) {
            PictureData picture = (PictureData) pictures.get(i);
            byte[] data = picture.getData();
            File file = new File("C:\\Users\\Administrator\\Desktop\\demoPic\\" + UUID.fastUUID() + ".jpg");
            @Cleanup
            FileOutputStream out = new FileOutputStream(file);
            result.picPathList.add(file.getAbsolutePath());
        }
        return result;
    }
}
